import {
  _decorator,
  Component,
  Node,
  UITransform,
  Sprite,
  Animation,
  SpriteFrame,
  AnimationClip,
  animation,
  utils,
} from 'cc'
const { ccclass, property } = _decorator

import {  TILE_WIDTH, TILE_HEIGHT } from '../Tile/TileMapManager'

// 数据中心
import DataManager from '../../Runtime/DataManager'

// 引入事件中心
import EventManager from '../../Runtime/EventManager'

// 引入状态
import { PLAYER_STATE_TYPE_ENUM } from '../../Enums'

// 实体类型
import { SpkiesEntityManager } from '../../Base/SpkiesEntityManager'

// 状态机
import { SpkiesStateMchine } from './SpkiesStateMchine'



@ccclass('SpkiesManager')
export class SpkiesManager extends SpkiesEntityManager {
  // id: string
  async init(pramers) {

    // 初始化状态机
    this.fsm = this.addComponent(SpkiesStateMchine)
    await this.fsm.init()

    // 传入默认值
    super.init(pramers)

    // 注册事件
    EventManager.Instance.on('PLAYER_MOVE_END', this.onLoop, this)
  }

  onDestroy(): void {
    super.onDestroy()
    // 移除
    EventManager.Instance.off('PLAYER_MOVE_END', this.onLoop)
  }

  // 不断循环
  onLoop() {
    //  达到最大值会在动画回调置0，当最大值时还没归零但人又触发移动，就让他变成1就好了
    if(this.count === this.totalCount) {
      this.count = 1
    } else {
      this.count++
    }

    // 攻击玩家
    this.onAttack()
  }
  // 回到初始状态
  backZero() {
    this.count = 0
  }

  // 攻击玩家
  onAttack() {
    const player = DataManager.Instance.player
    if(!player) return
    const { x:playerX, y:playerY } = player
    if(this.x === playerX && this.y === playerY && this.count === this.totalCount) {
      EventManager.Instance.emit('ATTACK_PLAYER', PLAYER_STATE_TYPE_ENUM.DEATH)
    }
  }
}
